<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="padding-top:30px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="memberName" label="反馈人：">
          <el-input v-model="filters.memberName" placeholder="反馈人"></el-input>
        </el-form-item>
        <el-form-item prop="startTime" label="反馈时间："  label-width="100px">
          <el-date-picker
              v-model="filters.startTime"
              type="datetime"
              placeholder="开始时间"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              clearable
              @change="changeStartDate"
              :picker-options="startDateDisabled"
            ></el-date-picker>
        </el-form-item>
        <span class="times-to">-</span>
        <el-form-item prop="endTime">
           <el-date-picker
                v-model="filters.endTime"
                type="datetime"
                placeholder="结束时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                clearable
                @change="changeEndDate"
                :picker-options="endDateDisabled"
              ></el-date-picker>
        </el-form-item>
        <el-row style="text-align:right">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button :label="$t('action.reset')" type="primary" @click="resetForm('filters')"/>
          </el-form-item>
          <el-form-item >
            <el-button-group>
              <el-tooltip content="刷新" placement="top">
                <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
              </el-tooltip>
              <el-tooltip content="列显示" placement="top">
                <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
              </el-tooltip>
              <el-tooltip content="导出" placement="top">
                <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
              </el-tooltip>
            </el-button-group>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      @findPage="findPage"
      :customLabel='"回复"'
      :showCustomOperation="true"
      :showEditOperation="false"
      :showDetailOperation="true"
      @handleCustom="handleEdit"
      @handleDelete="handleDelete"
      @handleDetail="handleDetail"
      ref="CyTable"
      :operationWidth="300"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'回复'"
      width="40%"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="80px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
      >
        <el-form-item label="id" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="回复内容" prop="replyContent">
          <el-input v-model="dataForm.replyContent" auto-complete="off" type="textarea" class="setTextareaHight" 
          maxlength="500" show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>

    <!--详情界面-->
    <el-dialog
      title="详情"
      width="45%"
      :visible.sync="editDialogVisibleDetail"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataFormDetail"
        label-width="100px"
        ref="dataForm"
        :size="size"
      >
        <el-form-item label="id" prop="id" v-if="false"  >
          <el-input v-model="dataFormDetail.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="反馈人：" prop="memberName" class="w325">
          <el-input v-model="dataFormDetail.memberName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="反馈时间：" prop="createTime" class="w325">
          <el-input v-model="dataFormDetail.createTime" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="反馈内容：" prop="content">
          <div class="contentStyle" v-html="dataFormDetail.content"></div>
        </el-form-item>
        <el-form-item label="回复内容：" prop="replyContent">
          <div class="contentStyle" v-html="dataFormDetail.replyContent"></div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="editDialogVisibleDetail = false">{{$t('action.cancel')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import CyTable from "@/views/Core/JicTable";
  import KtButton from "@/views/Core/KtButton";
  import {format} from "@/utils/datetime";
  import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";

  export default {
    components: {
      CyTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        startDateDisabled: {},//开始时间限制
        endDateDisabled: {},//结束时间限制
        size: "small",
        filters: {
        },
        columns: [
          {prop: "id", label: "序号", minWidth: 50},
          {prop: "memberName", label: "反馈人", minWidth: 100},
          {prop: "content", label: "反馈内容", minWidth: 100, formatter: this.contentFormat},
          {prop: "createTime", label: "反馈时间", minWidth: 120},
          {prop: "status", label: "状态", minWidth: 120,formatter: this.statusFormat},
          {prop: "replyName", label: "回复人", minWidth: 120},
          {prop: "replyContent", label: "回复内容", minWidth: 120,formatter: this.replyFormat},
          {prop: "replytime", label: "回复时间", minWidth: 120}
        ],
        filterColumns: [],
        pageRequest: {pageNum: 1, pageSize: 10},
        pageResult: {},
        operation: false, // true:新增, false:编辑
        editDialogVisible: false, // 新增编辑界面是否显示
        editLoading: false,
        editDialogVisibleDetail: false,
        dataFormRules: {
          replyContent: [{required: true, message: "回复内容不得为空", trigger: "blur"}]
        },
        // 新增编辑界面数据
        dataForm: {
          replyContent:''
        },
        dataFormDetail: {}
      };
    },
    methods: {
    changeStartDate: function() {
      var this_ = this;
      this_.endDateDisabled = {
        disabledDate: time => {
          if (this_.filters.startTime) {
            return time.getTime() <= new Date(this_.filters.startTime).getTime();
          }
        }
      };
    },
    changeEndDate: function() {
      var this_ = this;
      this_.startDateDisabled = {
        disabledDate: time => {
          if (this_.filters.endTime) {
            return time.getTime() >= new Date(this_.filters.endTime).getTime();
          }
        }
      };
    },
      // 获取分页数据
      findPage: function(data) {
        if(data != null && data.start != null){
        if(data.start != null){
          this.pageRequest.pageNum = data.start;
        }
        if(data.limit != null){
          this.pageRequest.pageSize = data.limit;
        }
       }
        let this_ = this;
        if (this_.filters == undefined || this_.filters == null) {
          this_.filters = {};
        }
        this_.filters.start = this.pageRequest.pageNum;
        this_.filters.limit = this.pageRequest.pageSize;
        this.utils.request.queryFeedBackList(this_.filters, function (res) {
          if (res.data == null) {
              res.data = [];
              res.totle = 0;
          }else{
              this_.$refs.CyTable.findPage({content:res.data.rows,total:res.data.total});
          }
        });
      },
      // 批量删除
      handleDelete: function (data) {
        if (data != null && data.params != null && data.params.length > 0) {
          let ids = data.params.map(item => item.id).toString();
          let params = {};
          params.ids = ids;
          var this_ = this;
          this.utils.request.deleteFeedBackInfo(params, function (res) {
            if (res.code == "0000") {
              this_.$message({message: "操作成功", type: "success"});
              this_.findPage(null);
            } else {
              this_.$message({message: "操作失败, " + res.msg, type: "error"});
            }
          });
        }
      },
      // 显示编辑界面
      handleEdit: function (params) {
        this.editDialogVisible = true;
        this.operation = false;
        this.$nextTick(function () {
          this.dataForm = Object.assign({}, params.row);
        });
      },
      // 显示编辑界面
      handleDetail: function (params) {
        this.editDialogVisibleDetail = true;
        this.$nextTick(function () {
          this.dataFormDetail = Object.assign({}, params.row);
        });
      },
      // 编辑
      submitForm: function () {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.$confirm("确认提交吗？", "提示", {}).then(() => {
              let this_ = this;
              let params = Object.assign({}, this_.dataForm);
              this.utils.request.editFeedBackInfo(params, function (res) {
                if (res.code == "0000") {
                  this_.$message({message: "操作成功", type: "success"});

                  this_.$refs["dataForm"].resetFields();

                  this_.editDialogVisible = false;

                  this_.findPage(null);
                } else {
                  this_.$message({
                    message: "操作失败, " + res.msg,
                    type: "error"
                  });
                }
              });
            });
          }
        });
      },
      statusFormat: function(row, column, cellValue, index){
        if(cellValue==0){
          return '待回复'
        }else if(cellValue==1){
          return '已回复'
        }
      },

      resetForm(formName) {
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].resetFields();
          this.filters.startTime = "";
          this.filters.endTime = "";
          this.findPage(null);
        }
      },

      // 处理表格列过滤显示
      displayFilterColumnsDialog: function () {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      // 处理表格列过滤显示
      initColumns: function () {
        let temp = [];

        $.each(this.columns, function (key, val) {
          temp.push(val);
        });
        this.filterColumns = temp;
      },
      //列表下载
      downloadExcel() {
        this.$confirm("确定下载列表文件?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            let filename = "意见反馈下载";
            this.$refs.CyTable.exportData(
              this.filters,
              this.filterColumns,
              filename
            );
          })
          .catch(() => {
          });
      },
      contentFormat: function (row, column) {
        let content = "";
        if (row.content != null && row.content != "") {
          content = row.content.substr(0, 20) + "......";
        }
        return content;
      },
      replyFormat: function (row, column) {
        let replyContent = "";
        if (row.replyContent != null && row.replyContent != "") {
          replyContent = row.replyContent.substr(0, 20) + "......";
        }
        return replyContent;
      }
      
    },
    mounted() {
      this.initColumns()
    }
  };
</script>

<style lang="scss" scoped>
/deep/ .setTextareaHight .el-textarea__inner {
  height: 180px;
}
</style>
